// we7/pages/index/BonusShopDetail.js
const app=getApp();
var WxParse = require('../../../wxParse/wxParse.js'); 

Page({
  
  /**
   * 页面的初始数据
   */
  data: { 
    block:0,
    data:[],
    goods_name:"",
    goods_describe:"",
    service_ensure: "",
    goods_present_price:"",
    goods_original_price:'',
    num: 1,
    minusStatus: 'disabled',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    imgUrls: [],
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    circular:true,
    good_image:"",
    nowprice:"",
    oldprice:"",
    stock:"",
    dataSon:[],
    placeChoose:true,
    // moGuiGe:"",
    // moGuiGeEnd:'',
    goods_id:'',
    specification_id:'',
    specification_name:"",
    sonsList:[],
    goods_total:"",
    evaluate: "",
    evaluateStatus:false,
    avatarUrl: "", 
    nickname: "",
    goods_evaluate_star: "",
    goods_evaluate_content: "",
    time: ""
  },
  showModal(e) {
    console.log(e)
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    console.log(e)
    this.setData({
      modalName: null
    })
  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function (){
    var that = this;
    var num = that.data.num;
    num++;
    var index = that.data.block;
    var data = that.data.sonsList;
    var goods_inventory = data[index]['goods_inventory'];
    if (num > goods_inventory){
      num = goods_inventory;
    }
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    that.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  inputTyping:function(e){
    var that = this;
    var reg = /^[1-9]\d*$/;
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    if (reg.test(e.detail.value)){
      var num = e.detail.value;
      var index = that.data.block;
      var data = that.data.sonsList;
      var goods_inventory = data[index]['goods_inventory'];
      if (num > goods_inventory) {
        num = goods_inventory;
      }
      
      that.setData({
        num: num,
        minusStatus: minusStatus
      });
    }else{
      that.setData({
        num: 1,
        minusStatus: minusStatus
      });
    }
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回
    this.setData({
      num: num
    });
  },
  changeIndicatorDots(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title:"商品详情"
    })
    var that = this;
    
    app.util.request({
      url: 'admin/WxGoodsApi/goodsInfo',
      data: {
        id: options.id
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res){ 
        console.log(res)
        if(res.data.code==0){
          var sonsList = res.data.data.son;

          that.setData({
            sonsList: sonsList,
            goods_id: res.data.data.id,
            specification_id: res.data.data.son[0].id,
            specification_name: res.data.data.son[0].specification_name
          })
          var evaluate = res.data.evaluate;
          if (evaluate) {
            var arr = [];
            for (var i = 0; i < evaluate.goods_evaluate_star; i++) {
              arr[i] = i + 1;
            }
            that.setData({
              evaluate: evaluate,
              avatarUrl: evaluate.avatarUrl,
              nickname: evaluate.nickname,
              goods_evaluate_star: arr,
              goods_evaluate_content: evaluate.goods_evaluate_content,
              time: evaluate.time,
            })

          } else {
            that.setData({
              evaluateStatus: true,
            })
          }
          var data = res.data.data
          for (var i = 0; i < data.length; i++) {
            data[i]['goods_list_image'] = app.globalData.url + data[i]['goods_list_image'];
          }
          var str = data.goods_rotation;
          var strs = new Array(); //定义一数组 
          strs = str.split(","); //字符分割
          for (var i = 0; i < strs.length; i++) {
            strs[i] = app.globalData.url + strs[i];
          }
          var dataSon = data.son
          var article = data.goods_introduce;
          // 参数说明 富文本
          WxParse.wxParse('article', 'html', article, that, 20);

          that.setData({
            data: data,
            dataSon: dataSon,
            good_image: app.globalData.url + dataSon[0]["goods_image"],
            nowprice: dataSon[0]['goods_present_price'],
            oldprice: dataSon[0]['goods_original_price'],
            stock: dataSon[0]["goods_inventory"],
            imgUrls: strs,
            goods_name: data.goods_name,
            goods_describe: data.goods_describe,
            service_ensure: data.service_ensure,
            goods_total: data.goods_total,
            goods_present_price: dataSon[0]['goods_present_price'],
            goods_original_price: dataSon[0]['goods_original_price'],
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.reLaunch({
              url: './index',
            })
          }, 2000)
        }
        
      },
      fail:function(res){
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //商品规格选择
  guigecheck:function(data){
    var that=this;
    console.log(data)
    that.setData({
      // moGuiGe: data._relatedInfo.anchorRelatedText,
      specification_id: data.currentTarget.dataset.specificationid,
      // specification_name: data.currentTarget.dataset.specificationName
    })
    var index = data.currentTarget.dataset.id;
    var data=that.data.data;
    var dataSon = data.son;
    var guigearr=dataSon[index];
    var num = 1;
 
        console.log(that.data.sonsList);
        console.log(that.data.sonsList[index].specification_name)
        that.setData({
          specification_name: that.data.sonsList[index].specification_name
        })
      
    that.setData({
      num:num,
      block:index,
      good_image: app.globalData.url + guigearr["goods_image"],
      nowprice: guigearr['goods_present_price'],
      oldprice: guigearr['goods_original_price'],
      stock: guigearr["goods_inventory"],
    })
  },
  // 商品规格
  showChoose: function (e) {
    this.showModal(e)
  },
  // 商品规格确认按钮
  placeSure: function (e) {
    console.log(e.detail.formId)
    var form_id = e.detail.formId
      var that = this;
      const value = wx.getStorageSync('openid');
      app.util.request({
        url: 'admin/WxGoodsApi/formId',
        method: 'post',
        data:{
          form_id:form_id,
          open_id: value
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          if (res.data.code == 0) {
            console.log("formId发送成功")
          }

        },
        fail: function (res) {
          wx.showToast({
            title: '服务器响应失败',
            icon: 'none',
            duration: 2000
          })
        }
      });
    this.hideModal(e)
    console.log(that.data.specification_name)
    this.setData({
      specification_name: that.data.specification_name,
      // moGuiGeEnd: this.data.moGuiGe,
      placeChoose: false
    })
  },
  // 加入购物车
  addShop:function(e){
    const value = wx.getStorageSync('openid')
    this.hideModal(e)
    // console.log(this.data.num)
    var that = this;
    var form_id = e.detail.formId
    app.util.request({
      url: 'admin/WxGoodsApi/formId',
      method: 'post',
      data: {
        form_id: form_id,
        open_id: value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          console.log("formId发送成功")
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
    app.util.request({
      url: 'admin/WxGoodsApi/shopcar',
      data: {
        goods_id: that.data.goods_id,
        goods_num:that.data.num,
        specification_id: that.data.specification_id,
        uid: value
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if(res.data.code==0){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
        
      },
      fail:function(res){
          wx.showToast({
            title: "服务器无响应",
            icon: 'none',
            duration: 2000
          })
      }
    });
    
  },
  // 立即购买
  submitSure:function(e){
    var that=this
    var form_id = e.detail.formId
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/formId',
      method: 'post',
      data: {
        form_id: form_id,
        open_id: value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          console.log("formId发送成功")
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
    var goods_id = that.data.goods_id
    var specification_id = that.data.specification_id
    var goods_num = that.data.num
    // console.log(goods_id)
    // console.log(specification_id)
    // console.log(goods_num)
    wx.redirectTo({
      url: 'XSMSIntegralExchange?goods_id='+goods_id+'&specification_id='+specification_id+'&goods_num='+goods_num
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})